<?php

require_once ABSPATH . '/system/sql.php';

$sql_link = MySQL::getInstance();


if($_GET['action'] == "ranking"){
    $page = $_GET['page'];
    $size = $_GET['size'];

    if($_GET['time'] == 'day'){
        $time = date("Y-m-d",time());
    }else if($_GET['time'] == 'week'){
        $now_time = time();
        $day = date("w",$now_time);
        if($day == 0){
            $day = 7;
        }
        $time = date("Y-m-d",strtotime('-'.($day-1).' day'));
    }else if($_GET['time'] == 'month'){
        $time = date("Y-m",time());
        $time .='-01';
    }
    $start_time = strtotime($time);
    

    $res = $sql_link->prepare('SELECT user_id,UNIX_TIMESTAMP(start_time) AS start_time,
    UNIX_TIMESTAMP(end_time) AS end_time FROM statistical_data WHERE end_time >= :time');
    $res->bindParam(':time',$time);
    $res->execute();
    $datas = $res->fetchAll(PDO::FETCH_ASSOC);

    $res_data = array();
    foreach($datas as $data){
        if(!isset($res_data[$data['user_id']])){
            $res_data[$data['user_id']]['user_id'] = $data['user_id'];
        }
        if($data['start_time'] < $start_time){
            $res_data[$data['user_id']]['time'] += $data['end_time'] - $start_time;
        }else{
            $res_data[$data['user_id']]['time'] += $data['end_time'] - $data['start_time'];
        }
    }
    $all_len = count($res_data);

    array_multisort(array_column($res_data,'time'),SORT_DESC,SORT_NUMERIC,$res_data);

    $ret_data = array_slice($res_data, ($page-1)*$size,$size);

    $len = count($ret_data);
    $str = 'SELECT id,name FROM users WHERE id IN(';
    for($i=0;$i<$len;$i++)
    {
        $str.='?,';
    }
    $str = rtrim($str, ',');
    $str = $str.')';
    $res = $sql_link->prepare($str);
    $index = 1;
    foreach($ret_data as $data){
        $res->bindValue($index,$data['user_id']);
        $index ++;
    }

    $res->execute();
    $users_datas = $res->fetchAll(PDO::FETCH_ASSOC);

    foreach($res_data as $index=>$data){
        foreach($users_datas as $user){
            if($data['user_id'] == $user['id']){
                $res_data[$index]['name'] = $user['name'];
                break;
            }
        }
    }

    $ret = array(
        'code' => 200,
        'msg' => 'ok',
        "pages"=> ceil($all_len / $size),
        "data"=> $res_data
    );

}else if($_GET['action'] == "people"){
    $res = $sql_link->prepare('SELECT user_id,UNIX_TIMESTAMP(start_time) AS start_time,
    UNIX_TIMESTAMP(end_time) AS end_time FROM statistical_data WHERE end_time >= :time');

    $res->bindParam(':time',$time);
    $now_time = time();
    
    $hour = date("H",$now_time);
    $hour = (floor($hour /2)+1) * 2;
    if($hour == 24){
        $hour = 0;
        $time = date("Y-m-d",strtotime("-1 day",$now_time));
    }else{
        $time = date("Y-m-d",strtotime("-2 day",$now_time));
    }
    $time = $time.' '.$hour.':00:00';
    
    $res->execute();
    $datas = $res->fetchAll(PDO::FETCH_ASSOC);

    $start_time = strtotime($time);

    foreach($datas as $data){
        $index_start = floor( ($data['start_time'] - $start_time) / 7200 );
        if($index_start < 0){
            $index_start = 0;
        }
        $index_end = floor( ($data['end_time'] - $start_time) / 7200 );
        while($index_start <= $index_end){
            $res_data[$index_start][$data['user_id']] = 0;
            $index_start++;
        }
    }

    for($index = 0; $index < 24;$index++){
        if(!isset($res_data[$index])){
            $res_data[$index] = array();
        }
        $ret_data[$index] = count( $res_data[$index] );
    }

    $ret = array(
        'code' => 200,
        'msg' => 'ok',
        "start"=> $time,
        "data"=> $ret_data
    );
}else if($_GET['action'] == "time"){
    $res = $sql_link->prepare('SELECT user_id,UNIX_TIMESTAMP(start_time) AS start_time,
    UNIX_TIMESTAMP(end_time) AS end_time FROM statistical_data WHERE end_time >= :time');

    $res->bindParam(':time',$time);

    $time = date("Y-m-d",strtotime("-14 day"));
    
    $res->execute();
    $datas = $res->fetchAll(PDO::FETCH_ASSOC);

    $start_time = strtotime($time);
    foreach($datas as $data){
        $index_start = floor( ($data['start_time'] - $start_time) / 86400 );
        $index_end = floor( ($data['end_time'] - $start_time) / 86400 );

        if($index_start < 0){
            $index_start = 0;
            $data['start_time'] = $start_time;
        }
        
        if($index_start == $index_end){
            $res_data[$index_start] += $data['end_time'] - $data['start_time'];
        }else{
            $res_data[$index_start] += ( $start_time + ($index_start+1)*86400 ) - $data['start_time'];
            for($index = $index_start + 1; $index < $index_end;$index++){
                $res_data[$index] += 86400;
            }
            $res_data[$index_end] += $data['end_time'] - ( $start_time + $index_end * 86400 );
        }
    }

    for($index = 0; $index < 15;$index++){
        if(!isset($res_data[$index])){
            $res_data[$index] = 0;
        }
        $ret_data[$index] = round($res_data[$index] / 3600,1);
    }

    $ret = array(
        'code' => 200,
        'msg' => 'ok',
        "start"=> $time,
        "data"=> $ret_data
    );
}

echo json_encode($ret);